#![warn(unreachable_code)]
use usrtrust::{UTime,SysTime,SysConvertor,Clocker,CpuClock,UClock,UClockExchange};
use usrtrust::{ULogs,Environment,SysBlocks};
use usrtrust::{PLLExchange,PLL,ClockTracer,PLLex};
use std::path::{Path};
use std::io::{Write,Result};
use std::thread;
use std::time::Duration;

fn main() -> Result<()> {
    let e = Environment::from_dir(Path::new("/tmp/usrtos"))?;
    let logt = ULogs::from_env(&e);
    let fsys = e.find("system_block")?;
    println!("{}",fsys);
    let mut sys = SysBlocks::new(fsys)?;
    let mut clkex = sys.get_items::<UClockExchange>("clock_a")?;
    let mut pllex = sys.get_items::<PLLExchange>("pll_parameter")?;
    let mut uclock = UClock::<CpuClock>::from_data(&mut clkex);
    let mut ts = PLLex::default();
    PLL::to_data(ts.l1.get_a(),&mut pllex);
    // let mut c = 0;
    loop {
        ts.set_filter(&mut pllex);
        let (d0,d1) = (ts.l0.getdata(),ts.l1.getdata());
        println!("ls:{:>20},pll:{:>20},|{:24e}",d0.rate_nano,d1.rate_nano,d1.rate_nano-d0.rate_nano);
        writeln!(*logt.info(),"{:?} {:?}",ts.l1,ts.l0.getdata());
        match ts.one_ls() {
            Some(uc) => {
                uclock=uc;
                uclock.to_data(&mut clkex);
            },
            None => {},
        }
    }
    Ok(())
}